
package splash.core.logging;

import java.io.File;
import java.io.PrintStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Logger
{
	private final String D_DIR = "logs";
	private final String ERROR_S = "";
	private final String WARNING_S = "";
	private final String INFO_S = "";
	
	private String ps; //path seperator
	private PrintStream p;

	public Logger()
	{
		ps = File.separator;
		
		//ime nove datoteke
		Date d = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd");
		String fName = "log_" + df.format(d);
		fName += ".log";
		
		try
		{
			//preveri, če obstaja mapa
			File dir = new File(D_DIR);
			
			if(!dir.isDirectory())
			{
				dir.mkdir();
			}
			
			OutputStream os = new FileOutputStream(D_DIR + ps + fName, true);
			p = new PrintStream(os, true);
			
		}
		catch(IOException e)
		{
			p = System.out;
			p.println("Nekaj je šlo narobe. Izpisujem na zaslon.");
			p.println();
			e.printStackTrace();
		}
	}
	
	public void close()
	{
		try
		{
			if(!p.equals(System.out) && p != null)
			{
				p.flush();
				p.close();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
	}
	
	public void setStream(PrintStream p)
	{
		if(p != null && p instanceof PrintStream)
			this.p = p;
	}
	
	public void printWarning(String s)
	{
		p.println(WARNING_S + s);
	}
	
	public void printError(String s)
	{
		p.println(ERROR_S + s);
	}
	
	public void printInfo(String s)
	{
		p.println(INFO_S + s);
	}
	
	public void print(String s)
	{
		p.println(s);
	}
}